package cc.rengu.redp.bizimpl.upmp.service.impl;

import cc.rengu.redp.bizimpl.constant.UpmpConstant;
import cc.rengu.redp.bizimpl.upmp.domain.ManagerInfo;
import cc.rengu.redp.bizimpl.upmp.domain.MchntApplyInfo;
import cc.rengu.redp.bizimpl.upmp.dao.MchntApplyInfoMapper;
import cc.rengu.redp.bizimpl.upmp.domain.RegionInfo;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpOrganization;
import cc.rengu.redp.bizimpl.upmp.service.*;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.Common;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import cc.rengu.redp.common.utils.SortUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author 刘玉亮.LL
 */
@Service
public class MchntApplyInfoServiceImpl extends ServiceImpl<MchntApplyInfoMapper, MchntApplyInfo> implements IMchntApplyInfoService {

    @Autowired
    private IUpmpSysParamService upmpSysParamService;

    @Autowired
    private RegionInfoService regionInfoService;

    @Autowired
    private IUpmpOrganizationService organizationService;

    @Autowired
    private IManagerInfoService managerInfoService;
    @Override
    public IPage<MchntApplyInfo> findByPage(QueryRequest request, MchntApplyInfo info) {
        try {
            LambdaQueryWrapper<MchntApplyInfo> queryWrapper = new LambdaQueryWrapper<>();
           // 当前登录用户
            UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
            String instId = info.getInstId();
            if (null != user && user.isAdmin()) {
                if (StringUtils.isBlank(instId)) {
                    instId = user.getInstId();
                    queryWrapper.likeRight(MchntApplyInfo::getInstId, info.getInstId());
                }
            } else {
                instId = user.getInstId();
            }
            queryWrapper.likeRight(MchntApplyInfo::getInstId, instId);
            if (StringUtils.isNotBlank(info.getApplyStatus())) {
                queryWrapper.likeRight(MchntApplyInfo::getApplyStatus, info.getApplyStatus());
            }
            if (StringUtils.isNotBlank(info.getContactPhone())) {
                queryWrapper.likeRight(MchntApplyInfo::getContactPhone, info.getContactPhone());
            }
            if (StringUtils.isNotBlank(info.getContactName())) {
                queryWrapper.likeRight(MchntApplyInfo::getContactName, info.getContactName());
            }
            if (StringUtils.isNotBlank(info.getProvince())) {
                queryWrapper.likeRight(MchntApplyInfo::getProvince, info.getProvince());
            }
           if (StringUtils.isNotBlank(info.getCity())) {
                queryWrapper.likeRight(MchntApplyInfo::getCity, info.getCity());
            }
           if (StringUtils.isNotBlank(info.getArea())) {
                queryWrapper.likeRight(MchntApplyInfo::getArea, info.getArea());
            }
            Page<MchntApplyInfo> page = new Page<>();
            SortUtil.handlePageSort(request, page, true);
            Page<MchntApplyInfo> page1 = this.page(page, queryWrapper);
            for(MchntApplyInfo mchntApplyInfo:page1.getRecords()){
                mchntApplyInfo.setApplyTime(mchntApplyInfo.getApplyTime().substring(0,18));
                if(Common.isNotBlank(mchntApplyInfo.getArea())){
                    RegionInfo regionInfo=regionInfoService.findOneByCode(mchntApplyInfo.getArea());
                    if(regionInfo!=null){
                        String areaName=regionInfo.getRegionName();
                        mchntApplyInfo.setAreaName(areaName);
                    }
                }
                if(Common.isNotBlank(mchntApplyInfo.getInstId())){
                    //获取指定机构名称
                    String  instName=upmpSysParamService.findByPrimaryKey(mchntApplyInfo.getInstId(),upmpSysParamService.getById(UpmpConstant.IGAS_DEFAULT_INST_ID).getPrimaryValue(),"599").getPrimaryValue();
                    mchntApplyInfo.setInstName(instName);
                }
                if(Common.isNotBlank(mchntApplyInfo.getManagerId())){
                    List<ManagerInfo> managerInfoLst = managerInfoService.findManagerInfoByManagerId(mchntApplyInfo.getManagerId());
                    if(null != managerInfoLst && managerInfoLst.size() > 0){
                        UpmpOrganization organization = organizationService.getOrgByCode(managerInfoLst.get(0).getSignInstId(), mchntApplyInfo.getInstId());
                        if(!ObjectUtils.isEmpty(organization)){
                            mchntApplyInfo.setOrgCode(organization.getOrgCode());
                            mchntApplyInfo.setRemark3(organization.getOrgName());
                        }
                    }
                }
            }
            return page1;
        } catch (Exception e) {
            log.error("查询失败", e);
            return null;
        }
    }
}
